﻿using System.Threading.Channels;

namespace nhCore.nMessage;

public class NMessage
{
    public const int con_information = 0;
    public const int con_warn = 1;
    public const int con_error = 2;    

    public string Text = null!;
    public int Level;


    public static Channel<NMessage> MsgChannel { get; } = Channel.CreateBounded<NMessage>(
            new BoundedChannelOptions(100)
            {
                SingleWriter = false,
                SingleReader = true,
                FullMode = BoundedChannelFullMode.Wait
            });

    /// <summary>
    /// 普遍信息
    /// </summary>
    /// <param name="text"></param>
    public static void Message(string text)
    {
        MsgChannel.Writer.TryWrite(
            new NMessage()
            {
                Text = text,
                Level = NMessage.con_information,
            });
    }

    /// <summary>
    /// 警告信息
    /// </summary>
    /// <param name="text"></param>
    public static void Warn(string text)
    {
        MsgChannel.Writer.TryWrite(
            new NMessage()
            {
                Text = text,
                Level = NMessage.con_warn,
            });
    }

    /// <summary>
    /// 错误信息
    /// </summary>
    /// <param name="text"></param>
    public static void Error(string text)
    {
        MsgChannel.Writer.TryWrite(
            new NMessage()
            {
                Text = text,
                Level = NMessage.con_error,
            });
    }
}
